package com.starhub.utils;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.gson.reflect.TypeToken;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import java.lang.reflect.Type;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class Util {
    public static final String DBTYPE = "mysql";

    public Util() {
    }

    public static String null2String(Object s) {
        return s == null ? "" : s.toString();
    }

    public static Map null2Map(Map map) {
        return (Map)(map == null ? new HashMap() : map);
    }

    public static int getIntValue(String v) {
        return getIntValue((String)v, -1);
    }

    public static int getIntValue(String v, int def) {
        try {
            return Integer.parseInt(v);
        } catch (Exception var3) {
            return def;
        }
    }

    public static int getIntValue(Object obj, int def) {
        try {
            return Integer.parseInt(null2String(obj));
        } catch (Exception var3) {
            return def;
        }
    }

    public static int getIntValue(Object obj) {
        return getIntValue((Object)obj, -1);
    }

    public static Integer number2Integer(Number number) {
        return number == null ? null : number.intValue();
    }

    public static boolean getBooleanValue(String v) {
        return getBooleanValue(v, false);
    }

    public static boolean getBooleanValue(String v, boolean def) {
        try {
            if ("1".equals(v)) {
                v = "true";
            } else if ("0".equals(v)) {
                v = "false";
            }

            return Boolean.parseBoolean(v);
        } catch (Exception var3) {
            return def;
        }
    }

    public static Long getLongValue(Object obj) {
        return getLongValue(null2String(obj), -1L);
    }

    public static Long getLongValue(Object obj, long def) {
        try {
            String v = null2String(obj);
            if (v.indexOf(".") > 0) {
                v = v.substring(0, v.indexOf("."));
            }

            return Long.parseLong(v);
        } catch (Exception var4) {
            return def;
        }
    }

    public static Long getLongValue(String v) {
        return getLongValue(v, -1L);
    }

    public static Long getLongValue(String v, Long def) {
        try {
            if (v.indexOf(".") > 0) {
                v = v.substring(0, v.indexOf("."));
            }

            return Long.parseLong(v);
        } catch (Exception var3) {
            return def;
        }
    }

    public static long getLongValue(Long v, long def) {
        if (v == null) {
            return def;
        } else {
            try {
                return v;
            } catch (Exception var4) {
                return def;
            }
        }
    }

    public static long getLongValue(String v, long def) {
        try {
            if (v.indexOf(".") > 0) {
                v = v.substring(0, v.indexOf("."));
            }

            return Long.parseLong(v);
        } catch (Exception var4) {
            return def;
        }
    }

    public static int getIntValue(int v) {
        return v;
    }

    public static String null2String(String s) {
        return s == null ? "" : s;
    }

    public static String null2String(String s, String def) {
        return s == null ? (def == null ? "" : def) : s;
    }

    public static Double getDoubleValue(String v) {
        return getDoubleValue(v, -1.0);
    }

    public static Double getDoubleValue(String v, Double def) {
        try {
            return Double.parseDouble(v);
        } catch (Exception var3) {
            return def;
        }
    }

    public static String toSqlForSplitPage(String s) {
        if (s.indexOf("'\\'") == -1) {
            s = StringUtils.replace(s, "\\'", "'");
        }

        s = StringUtils.replace(s, "&lt;", "<");
        s = StringUtils.replace(s, "&gt;", ">");
        s = StringUtils.replace(s, "&amp;", "&");
        s = StringUtils.replace(s, "&quot;", "\"");
        return s;
    }

    public static String toHtmlForSplitPage(String s) {
        if (isEmpty(s)) {
            return s;
        } else {
            char[] c = s.toCharArray();
            int i = 0;
            StringBuffer buf = new StringBuffer();

            while(i < c.length) {
                char ch = c[i++];
                if (ch == '\'') {
                    buf.append("\\'");
                } else if (ch == '<') {
                    buf.append("&lt;");
                } else if (ch == '>') {
                    buf.append("&gt;");
                } else if (ch == '&') {
                    buf.append("&amp;");
                } else if (ch == '"') {
                    buf.append("&quot;");
                } else {
                    buf.append(ch);
                }
            }

            return buf.toString();
        }
    }

    public static String getUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    public static Map<String, Object> request2Map(HttpServletRequest request) {
        Map properties = request.getParameterMap();
        Map<String, Object> returnMap = new HashMap();
        Iterator<Map.Entry> entries = properties.entrySet().iterator();
        String name = "";

        for(Object value = null; entries.hasNext(); returnMap.put(name, value)) {
            Map.Entry entry = (Map.Entry)entries.next();
            name = (String)entry.getKey();
            Object valueObj = entry.getValue();
            if (null == valueObj) {
                value = null;
            } else if (valueObj instanceof String[]) {
                String[] values = (String[])((String[])valueObj);
                if (values.length == 1) {
                    value = values[0];
                } else {
                    value = values;
                }
            } else {
                value = valueObj.toString();
            }
        }

        return returnMap;
    }

    public static char getSeparator() {
        return '\u0002';
    }

    public static String add0(int v, int l) {
        long lv = (long)Math.pow(10.0, (double)l);
        return String.valueOf(lv + (long)v).substring(1);
    }

    public static boolean isEmptyList(List l) {
        return Objects.isNull(l) || l.size() == 0;
    }

    public static boolean isNotEmptyList(List l) {
        return !isEmptyList(l);
    }

    public static String converString(String strs) {
        if (StringUtils.isNotBlank(strs)) {
            String[] idStrs = strs.trim().split(",");
            if (null != idStrs && idStrs.length > 0) {
                StringBuffer sbf = new StringBuffer("");
                String[] var3 = idStrs;
                int var4 = idStrs.length;

                for(int var5 = 0; var5 < var4; ++var5) {
                    String str = var3[var5];
                    if (StringUtils.isNotBlank(str)) {
                        sbf.append("'").append(str.trim()).append("'").append(",");
                    }
                }

                if (sbf.length() > 0) {
                    sbf = sbf.deleteCharAt(sbf.length() - 1);
                    return sbf.toString();
                }
            }
        }

        return "";
    }

    public static boolean isEmpty(Object aObj) {
        if (Objects.isNull(aObj)) {
            return true;
        } else if (aObj instanceof String) {
            return isEmpty((String)aObj);
        } else if (aObj instanceof Long) {
            return isEmpty((Long)aObj);
        } else if (aObj instanceof Collection) {
            return isEmpty((Collection)aObj);
        } else if (aObj instanceof Map) {
            return isEmpty((Map)aObj);
        }else {
            return aObj instanceof CharSequence ? isEmpty((CharSequence)aObj) : isNull(aObj);
        }
    }

    public static boolean isNotEmpty(Object aObj) {
        return !isEmpty(aObj);
    }

    public static boolean isEmpty(Long aLong) {
        return aLong == null;
    }

    public static boolean isEmpty(Map m) {
        return m == null || m.size() == 0;
    }

    public static boolean isEmpty(Collection c) {
        return c == null || c.size() == 0;
    }

    public static boolean isEmpty(String aStr) {
        return aStr == null || aStr.trim().isEmpty();
    }

    public static boolean isEmpty(CharSequence cs) {
        return cs == null || cs.toString().trim().isEmpty();
    }

    public static String trim(String aStr) {
        return aStr == null ? "" : aStr.trim();
    }

    public static boolean isNull(Object oStr) {
        return Objects.isNull(oStr);
    }

    public static boolean equals(String str1, String str2) {
        return str1 != null ? str1.equals(str2) : str2 == null;
    }

    public static boolean equals(Long L1, Long L2) {
        return L1 != null ? L1.equals(L2) : L2 == null;
    }

    public static boolean equals(Object obj1, Object obj2) {
        boolean result;
        if (obj1 != null) {
            result = obj1.equals(obj2);
        } else {
            result = obj2 == null;
        }

        return result;
    }

    public static boolean equals(String str, Integer num) {
        return str.equals(String.valueOf(num));
    }

    public static boolean equals(Integer num1, Integer num2) {
        return compare(num1, num2);
    }

    public static boolean compare(Number num1, Number num2) {
        if (num1 == null && num2 == null) {
            return true;
        } else if (num1 != null && num2 != null) {
            return num1.doubleValue() == num2.doubleValue();
        } else {
            return false;
        }
    }

    public static boolean validId(Long id) {
        return id != null && id > 0L;
    }

    public static boolean equalsIgnoreCase(String str1, String str2) {
        return str1 != null ? str1.equalsIgnoreCase(str2) : str2 == null;
    }

    public static <T> T emptyOr(T obj, T other) {
        return isEmpty(obj) ? other : obj;
    }

    public static <T, R> R emptyOr(T obj, Function<T, R> func, R other) {
        return isEmpty(obj) ? other : emptyOr(func.apply(obj), other);
    }

    public static <T, R> List<T> mapList(Collection<R> list, Function<? super R, ? extends T> mapper) {
        return (List)(isEmpty(list) ? new ArrayList() : (List)list.stream().map(mapper).collect(Collectors.toList()));
    }

    public static <R, K, U> Map<K, U> toMap(Collection<R> list, Function<? super R, ? extends K> keyMapper, Function<? super R, ? extends U> valueMapper) {
        return (Map)(isEmpty(list) ? new HashMap() : (Map)list.stream().collect(Collectors.toMap(keyMapper, valueMapper, (v1, v2) -> {
            return v2;
        })));
    }

    public static <R, K, U> Map<K, U> toLinkedMap(Collection<R> list, Function<? super R, ? extends K> keyMapper, Function<? super R, ? extends U> valueMapper) {
        return (Map)(isEmpty(list) ? new LinkedHashMap() : (Map)list.stream().collect(Collectors.toMap(keyMapper, valueMapper, (v1, v2) -> {
            return v2;
        }, LinkedHashMap::new)));
    }

    public static <R, U> Map<String, U> toCaseInsensitiveMap(Collection<R> list, Function<? super R, String> keyMapper, Function<? super R, ? extends U> valueMapper) {
        return (Map)(isEmpty(list) ? new TreeMap(String.CASE_INSENSITIVE_ORDER) : (Map)list.stream().collect(Collectors.toMap(keyMapper, valueMapper, (v1, v2) -> {
            return v2;
        }, () -> {
            return new TreeMap(String.CASE_INSENSITIVE_ORDER);
        })));
    }

    public static <R, K> Map<K, List<R>> toGroup(Collection<R> list, Function<? super R, ? extends K> classifier, boolean keepOrder) {
        if (isEmpty(list)) {
            return new HashMap();
        } else {
            return keepOrder ? (Map)list.stream().collect(Collectors.groupingBy(classifier, LinkedHashMap::new, Collectors.toList())) : (Map)list.stream().collect(Collectors.groupingBy(classifier));
        }
    }

    public static void executeIf(boolean condition, Runnable action) {
        if (condition && isNotEmpty(action)) {
            action.run();
        }

    }

    public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
        Set<Object> seen = ConcurrentHashMap.newKeySet();
        return (t) -> {
            return seen.add(keyExtractor.apply(t));
        };
    }

    public static String toJSONString(Object o) {
        String str;
        try {
            str = JSONObject.toJSONString(o);
        } catch (Exception var3) {
            str = "";
        }

        return str;
    }

    public static List<Map<String, Object>> convertMapObjectToString(List<Map<String, Object>> list) {
        Type genericType = (new TypeToken<List<Map<String, Object>>>() {
        }).getType();
        list = (List)JSONObject.parseObject(JSONObject.toJSONString(list, new SerializerFeature[]{SerializerFeature.WriteDateUseDateFormat}), genericType, new Feature[0]);
        return (List)list.stream().map((map) -> {
            return (Map)map.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, (entry) -> {
                return null2String(entry.getValue());
            }));
        }).collect(Collectors.toList());
    }

    public static Map<String, String> convertMapToStringMap(Map<String, Object> objectMap) {
        Map<String, String> stringMap = new HashMap();
        Iterator var2 = objectMap.entrySet().iterator();

        while(var2.hasNext()) {
            Map.Entry<String, Object> entry = (Map.Entry)var2.next();
            String key = (String)entry.getKey();
            Object value = entry.getValue();
            stringMap.put(key, null2String(value));
        }

        return stringMap;
    }

    public static Float getFloatValue(Object obj) {
        return getFloatValue(null2String(obj), -1.0f);
    }

    public static Float getFloatValue(Object obj, float def) {
        try {
            String v = null2String(obj);
            return Float.parseFloat(v);
        } catch (Exception var4) {
            return def;
        }
    }

    public static Float getFloatValue(String v) {
        return getFloatValue(v, -1.0f);
    }

    public static Float getFloatValue(String v, Float def) {
        try {
            return Float.parseFloat(v);
        } catch (Exception var3) {
            return def;
        }
    }

    public static float getFloatValue(Float v, float def) {
        if (v == null) {
            return def;
        } else {
            try {
                return v;
            } catch (Exception var4) {
                return def;
            }
        }
    }

    public static float getFloatValue(String v, float def) {
        try {
            return Float.parseFloat(v);
        } catch (Exception var4) {
            return def;
        }
    }
}
